﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace CPPEI.Coolzon.Designer.Views
{
    public static class ThumbnailHelper
    {
        private static string ThumbnailPath
        {
            get
            {
                return UIContext.Current.LocalWorkSpace.LocalPath + UIContext.Current.CurrentSolution.ID + "/Thumbs/";
            }
        }

        public static void SaveToImage(Coolzon.UI.Part part,int width, int height, string sceneId)
        {
            if (!Directory.Exists(ThumbnailPath))
            {
                Directory.CreateDirectory(ThumbnailPath);
            }
            using (System.IO.FileStream fs = new System.IO.FileStream(string.Format(ThumbnailPath + "{0}.png", sceneId), System.IO.FileMode.Create))
            {
                RenderTargetBitmap bmp = new RenderTargetBitmap(width, height, 96d, 96d,
                PixelFormats.Default);
                bmp.Render(part);
                BitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(bmp));
                encoder.Save(fs);
            }
        }

        public static string GetThumbPath(string sceneId)
        {
            return string.Format(ThumbnailPath + "{0}.png", sceneId);
        }
    }
}
